﻿/*
 * Created by SharpDevelop.
 * User: rogerio
 * Date: 12/5/2009
 * Time: 10:01 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;

namespace csharp.rad.general.entries
{
	/// <summary>
	/// Description of frmRegisterProduct.
	/// </summary>
	public partial class frmRegisterProduct : csharp.common.RegisterTemplate
	{
		#region declarations
			private Product          product              = new Product();
			private Products         listProducts         = new Products();
			private ProductGroups    listProductGroups    = new ProductGroups();
			private ProductSubGroups listProductSubGroups = new ProductSubGroups();
			private ProductMarks     listProductMarks     = new ProductMarks();
			private ProductSections  listProductSections  = new ProductSections();
			private ProductTypes     listProductType      = new ProductTypes();
			private Suppliers		 listSuppliers		  = new Suppliers();
			
		#endregion
		public frmRegisterProduct()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			this.dgvMain.Size = new Size(565, 309);
		}
		
		#region overrided methods		
			protected override bool delete()
			{
				product.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
				return product.delete();
			}
			
			protected override void fillDataGridView()
			{
				int tam;
				
				listProducts.fillList();
				tam = listProducts.count();
				dgvMain.Rows.Clear();
				for(int i = 0; i < tam; i++)
				{
					dgvMain.Rows.Add(new String[] { listProducts.get(i).id,
					                 	            listProducts.get(i).Code,
					                 	            listProducts.get(i).Name,
					                 	            listProducts.get(i).Sale_price1,
					                 	            listProducts.get(i).Active});
				}
			}
			
			protected override void find()
			{
				csharp.common.Util.searchInGrid(dgvMain, "name", txtSearch.Text);
			}
			
			protected override void init()
			{
				DataGridViewTextBoxColumn id = new DataGridViewTextBoxColumn();
				id.Name = "id";
				id.Visible = false;
				
				DataGridViewTextBoxColumn code = new DataGridViewTextBoxColumn();
				code.Name = "code";
				code.HeaderText = "Código";
				code.Width = 50;
				
				DataGridViewTextBoxColumn name = new DataGridViewTextBoxColumn();
				name.Name = "name";
				name.HeaderText = "Nome";
				name.Width = 300;
				
				DataGridViewTextBoxColumn salePrice = new DataGridViewTextBoxColumn();
				salePrice.Name = "salePrice";
				salePrice.HeaderText = "Preço venda";
				
				DataGridViewCheckBoxColumn active = new DataGridViewCheckBoxColumn();
				active.Name = "active";
				active.HeaderText = "Ativo";	
				active.Width = 50;
				
				dgvMain.Columns.AddRange(new DataGridViewColumn[] {id, code, name, salePrice, active});
				

			}
			
			protected override void newEvent()
			{
				base.newEvent();
			}
			
			protected override bool save()
			{
				return product.save();
			}
			
			protected override void setClass()
			{
				
				String nature; 
				if(cbNature.Text == "Ativo") nature = "A";
				else if(cbNature.Text == "Revenda") nature = "R";
				else nature = "C";
				product.Name             = txtName.Text;
				product.Short_name       = txtShortName.Text;
				product.Bar_code         = txtBarCode.Text;
				product.Code             = txtCode.Text;
				product.Unit1            = cbxUnit1.Text;
				product.Unit2            = cbxUnit2.Text;
				product.Unit3            = cbxUnit3.Text;
				product.Conversion1      = txtConversion1.Text;
				product.Conversion2      = txtConversion2.Text;
				product.Service          = cbxService.Checked.ToString();
				product.Rent             = cbxRent.Checked.ToString();
				product.Service_order    = cbxServiceOrder.Checked.ToString();
				product.Image            = txtpath.Text;
				product.Stock_min        = txtStockMin.Text;
				product.Stock_max        = txtStockMax.Text;
				product.Weight           = txtWeight.Text;
				product.Duration_time    = txtDurationTime.Text;
				product.Fiscal_nature    = "T" ;//= cbNature.Text;
				product.Purchase_price   = txtPurchasePrice.Text;
				product.Sale_price1      = txtSalePrice1.Text;
				product.Sale_price2      = txtSalePrice2.Text;
				product.Sale_price3      = txtSalePrice3.Text;
				product.Nature           = nature; //cbNature.Text;
				product.Accept_discount  = cbxAcceptDiscount.Checked.ToString();
				product.Accept_fraction  = cbxAcceptFraction.Checked.ToString();
				product.User_bars        = cbxUserBars.Checked.ToString();
				product.Active			 = cbxActive.Checked.ToString();
				product.Alteration_date  = DateTime.Now.ToString();
				product.Ncm_code		 = txtNcmCode.Text;
				product.Nbm_code		 = txtNbmCode.Text;
				product.Obs                = txtObs.Text;
				product.Application		   = txtApplication.Text;;
				product.Product_group_id   = (cbGroup.SelectedItem as ProductGroup).id;
				product.Product_mark_id    = (cbMark.SelectedItem as ProductMark).id;
				product.Product_section_id = (cbSection.SelectedItem as ProductSection).id;
				product.Product_sub_group_id = (cbSubGroup.SelectedItem as ProductSubGroup).id;
				product.Product_type_id      = (cbType.SelectedItem as ProductType).id;
				product.Company_id           = Information.companyId;
				//product.En_tributary_situation_id = "";
				//TODO:Pegar id dos fornecedores
				if(cbxVendor.SelectedIndex >= 0)
					product.Vendor_id1           = (cbxVendor.SelectedItem as Supplier).id;
				if(cbxVendor2.SelectedIndex >= 0)
					product.Vendor_id2           = (cbxVendor2.SelectedItem as Supplier).id;
				if(cbxVendor3.SelectedIndex >= 0)
					product.Vendor_id3           = (cbxVendor3.SelectedItem as Supplier).id;
				
			}
			
			protected override void setEdits()
			{
				product.id = dgvMain.CurrentRow.Cells["id"].Value.ToString();
				
				if (product.get())
				{	
					int nature;
					if(product.Nature == "A") nature = 1;
				    else if(product.Nature == "R") nature = 2;
				    else nature = 3;
					txtName.Text              = product.Name  ; 
					txtShortName.Text         = product.Short_name ; 
					txtBarCode.Text           = product.Bar_code   ; 
					txtCode.Text              = product.Code  ; 
					txtConversion1.Text       = product.Conversion1; 
					txtConversion2.Text       = product.Conversion2; 
					txtpath.Text              = product.Image; 
					txtStockMin.Text          = product.Stock_min  ; 
					txtStockMax.Text          = product.Stock_max  ; 
					txtWeight.Text            = product.Weight; 
					txtDurationTime.Text      = product.Duration_time; 
					txtPurchasePrice.Text     = product.Purchase_price   ; 
					txtSalePrice1.Text        = product.Sale_price1; 
					txtSalePrice2.Text        = product.Sale_price2; 
					txtSalePrice3.Text        = product.Sale_price3;  
					txtNcmCode.Text           = product.Ncm_code ; 
					txtNbmCode.Text           = product.Nbm_code ; 
					txtObs.Text               = product.Obs ; 
					txtApplication.Text       = product.Application; 
					cbNature.SelectedIndex    = nature;
					cbxUnit1.Text             = product.Unit1 ; 
					cbxUnit2.Text             = product.Unit2 ; 
					cbxUnit3.Text             = product.Unit3 ; 
					cbxRent.Checked           = Convert.ToBoolean(product.Rent);
					cbxService.Checked        = Convert.ToBoolean(product.Service);
					cbxServiceOrder.Checked   = Convert.ToBoolean(product.Service_order);
					cbxAcceptDiscount.Checked = Convert.ToBoolean(product.Accept_discount);
					cbxAcceptFraction.Checked = Convert.ToBoolean(product.Accept_fraction);
					cbxUserBars.Checked       = Convert.ToBoolean(product.User_bars);
					cbxActive.Checked         = Convert.ToBoolean(product.Active);
					common.Util.setComboBox(listProductGroups, cbGroup, product.Product_group_id);
					common.Util.setComboBox(listProductMarks, cbMark, product.Product_mark_id);
					common.Util.setComboBox(listProductSections, cbSection, product.Product_section_id);
					common.Util.setComboBox(listProductSubGroups, cbSubGroup, product.Product_sub_group_id);
					common.Util.setComboBox(listProductType, cbType, product.Product_type_id);
					common.Util.setComboBox(listSuppliers, cbxVendor, product.Vendor_id1);
					common.Util.setComboBox(listSuppliers, cbxVendor2, product.Vendor_id2);
					common.Util.setComboBox(listSuppliers, cbxVendor3, product.Vendor_id3);
				}
			}
			
			protected override bool update()
			{
				return product.update();
			}
			
			protected override bool validate()
			{
				return base.validate();
			}
		#endregion
		
		private void fillComboGroup()
		{
			int tam = listProductGroups.count();
			
			cbGroup.Items.Clear();
			cbGroup.Text = " ";
			
			for(int i = 0; i < tam; i++)
			{
				cbGroup.Items.Add(listProductGroups.get(i));
			}
			cbGroup.ValueMember   = "id";
			cbGroup.DisplayMember = "name";
		}
		
		private void fillComboSubGroup()
		{
			int tam = listProductSubGroups.count();
			
			cbSubGroup.Items.Clear();
			cbSubGroup.Text = " ";
			
			for(int i = 0; i < tam; i++)
			{
				cbSubGroup.Items.Add(listProductSubGroups.get(i));
			}
			cbSubGroup.ValueMember   = "id";
			cbSubGroup.DisplayMember = "name";
		}
		
		private void fillComboMark()
		{
			int tam = listProductMarks.count();
			
			cbMark.Items.Clear();
			for(int i = 0; i < tam; i++)
			{
				cbMark.Items.Add(listProductMarks.get(i));
			}
			cbMark.ValueMember   = "id";
			cbMark.DisplayMember = "name";
		}
		
		private void fillComboSection()
		{
			int tam = listProductSections.count();
			
			cbSection.Items.Clear();
			for(int i = 0; i < tam; i++)
			{
				cbSection.Items.Add(listProductSections.get(i));
			}
			cbSection.ValueMember   = "id";
			cbSection.DisplayMember = "name";
		}
		
		private void fillComboType()
		{
			int tam = listProductType.count();
			
			cbType.Items.Clear();
			for(int i = 0; i < tam; i++)
			{
				cbType.Items.Add(listProductType.get(i));
			}
			cbType.ValueMember   = "id";
			cbType.DisplayMember = "name";
		}
		
		private void fillComboVendor()
		{
			int tam = listSuppliers.count();
			
			cbxVendor.Items.Clear();
			
			for(int i = 0; i < tam; i++)
			{
				cbxVendor.Items.Add(listSuppliers.get(i).person);
				cbxVendor2.Items.Add(listSuppliers.get(i).person);
				cbxVendor3.Items.Add(listSuppliers.get(i).person);
			}
			cbxVendor.ValueMember   = "id";
			cbxVendor.DisplayMember = "name";
			
			cbxVendor2.ValueMember   = "id";
			cbxVendor2.DisplayMember = "name";
			
			cbxVendor3.ValueMember   = "id";
			cbxVendor3.DisplayMember = "name";
			
		}
		
		void FrmRegisterProductLoad(object sender, EventArgs e)
		{
			listProductGroups.fillList();
			fillComboGroup();
			
			listProductSubGroups.fillList();
			fillComboSubGroup();
			
			listProductMarks.fillList();
			fillComboMark();
			
			listProductSections.fillList();
			fillComboSection();
			
			listProductType.fillList();
			fillComboType();
			
			listSuppliers.fillList();
			fillComboVendor();
		}
		
		void CbGroupClick(object sender, EventArgs e)
		{
			listProductSubGroups.fillListByGroup((cbGroup.SelectedItem as ProductGroup).id);
			fillComboSubGroup();
		}
		
		//void CbGroupSelectedIndexChanged(object sender, EventArgs e)
		//{
		//	listProductSubGroups.fillListByGroup((cbGroup.SelectedItem as ProductGroup).id);
		//	fillComboSubGroup();
		//}
		
		void BtnSearchImageClick(object sender, EventArgs e)
		{
			if(ofdImage.ShowDialog() == DialogResult.OK)
			{
				pbxImage.Image = Image.FromFile(ofdImage.FileName);
				txtpath.Text = ofdImage.FileName;
			}
		}
	}
}
